﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class OrderController
    {
        public NB_Store_OrdersInfo CreateEmptyOrder(string PortalID, int UserID)
        {
            NB_Store_OrdersInfo objInfo = new NB_Store_OrdersInfo();
            NB_Store_AddressInfo info2 = new NB_Store_AddressInfo();
            objInfo.PortalID = Conversions.ToInteger(PortalID);
            objInfo.OrderDate = DateAndTime.Now;
            objInfo.OrderID = -1;
            objInfo.OrderStatusID = 10;
            objInfo.UserID = UserID;
            objInfo = this.UpdateObjOrder(objInfo);
            info2.UserID = -1;
            info2.AddressID = -1;
            info2.OrderID = objInfo.OrderID;
            info2.CreatedDate = DateAndTime.Now;
            info2.CountryCode = SharedFunctions.GetMerchantCountryCode(Conversions.ToInteger(PortalID));
            info2 = this.UpdateObjOrderAddress(info2);
            objInfo.BillingAddressID = info2.AddressID;
            objInfo.ShippingAddressID = info2.AddressID;
            return this.UpdateObjOrder(objInfo);
        }

        public void DeleteOrder(int OrderID)
        {
            NB_Store_OrdersInfo order = this.GetOrder(OrderID);
            if ((order != null) && !order.OrderIsPlaced)
            {
                this.DeleteOrderAddress(order.BillingAddressID);
                if (order.BillingAddressID != order.ShippingAddressID)
                {
                    this.DeleteOrderAddress(order.ShippingAddressID);
                }
                DataProvider.Instance().DeleteNB_Store_Orders(OrderID);
            }
        }

        public void DeleteOrderAddress(int AddressID)
        {
            DataProvider.Instance().DeleteNB_Store_Address(AddressID);
        }

        public void DeleteOrderDetail(int OrderDetailID)
        {
            DataProvider.Instance().DeleteNB_Store_OrderDetails(OrderDetailID);
        }

        public void DeleteOrderStatus(int OrderStatusID, string Lang)
        {
            DataProvider.Instance().DeleteNB_Store_OrderStatus(OrderStatusID, Lang);
        }

        public NB_Store_OrdersInfo GetOrder(int OrderID)
        {
            return (NB_Store_OrdersInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_Orders(OrderID), typeof(NB_Store_OrdersInfo));
        }

        public NB_Store_AddressInfo GetOrderAddress(int AddressID)
        {
            return (NB_Store_AddressInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_Address(AddressID), typeof(NB_Store_AddressInfo));
        }

        public NB_Store_OrdersInfo GetOrderByGUID(string OrderGUID)
        {
            return (NB_Store_OrdersInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_OrdersByGUID(OrderGUID), typeof(NB_Store_OrdersInfo));
        }

        public NB_Store_OrderDetailsInfo GetOrderDetail(int OrderDetailID)
        {
            return (NB_Store_OrderDetailsInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_OrderDetails(OrderDetailID), typeof(NB_Store_OrderDetailsInfo));
        }

        public ArrayList GetOrderDetailList(int OrderID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_OrderDetailss(OrderID), typeof(NB_Store_OrderDetailsInfo));
        }

        public ArrayList GetOrderList(int PortalID, int UsrID)
        {
            return this.GetOrderList(PortalID, UsrID, DateAndTime.DateAdd(DateInterval.Year, -2.0, DateAndTime.Today), DateAndTime.Today, -1, "");
        }

        public ArrayList GetOrderList(int PortalID, int UsrID, int StatusID)
        {
            return this.GetOrderList(PortalID, UsrID, DateAndTime.DateAdd(DateInterval.Year, -2.0, DateAndTime.Today), DateAndTime.Today, StatusID, "");
        }

        public ArrayList GetOrderList(int PortalID, int UsrID, DateTime FromDate, DateTime ToDate, int StatusID, string Filter)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_Orderss(PortalID, UsrID, FromDate, ToDate, StatusID, Filter), typeof(NB_Store_OrdersInfo));
        }

        public ArrayList GetOrdersExportList(int PortalID, int StatusID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetOrdersExportList(PortalID, StatusID), typeof(NB_Store_OrdersInfo));
        }

        public NB_Store_OrderStatusInfo GetOrderStatus(int OrderStatusID, string Lang)
        {
            return (NB_Store_OrderStatusInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_OrderStatus(OrderStatusID, Lang), typeof(NB_Store_OrderStatusInfo));
        }

        public ArrayList GetOrderStatusList(string Lang)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_OrderStatuss(Lang), typeof(NB_Store_OrderStatusInfo));
        }

        public void PurgeArchivedOrders(int PortalID)
        {
            IEnumerator enumerator;
            ArrayList ordersExportList = this.GetOrdersExportList(PortalID, 0x4b);
            try
            {
                enumerator = ordersExportList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_OrdersInfo current = (NB_Store_OrdersInfo) enumerator.Current;
                    current.OrderIsPlaced = false;
                    this.UpdateObjOrder(current);
                    this.DeleteOrder(current.OrderID);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public NB_Store_OrdersInfo UpdateObjOrder(NB_Store_OrdersInfo objInfo)
        {
            return (NB_Store_OrdersInfo) CBO.FillObject(DataProvider.Instance().UpdateNB_Store_Orders(objInfo.OrderID, objInfo.UserID, objInfo.PortalID, objInfo.OrderNumber, objInfo.OrderDate, objInfo.ShipDate, objInfo.ShippingAddressID, objInfo.BillingAddressID, objInfo.AppliedTax, objInfo.ShippingCost, objInfo.OrderIsPlaced, objInfo.OrderStatusID, objInfo.PayType, objInfo.CalculatedTax, objInfo.NoteMsg, objInfo.VATNumber, objInfo.Discount, objInfo.PromoCode, objInfo.Total, objInfo.Email, objInfo.BankAuthCode, objInfo.ShipMethodID, objInfo.TrackingCode, objInfo.Stg2FormXML, objInfo.Stg3FormXML, objInfo.AlreadyPaid, objInfo.OrderGUID, objInfo.ElapsedDate, objInfo.GatewayProvider, objInfo.CartXMLInfo, objInfo.Referrer), typeof(NB_Store_OrdersInfo));
        }

        public NB_Store_AddressInfo UpdateObjOrderAddress(NB_Store_AddressInfo objInfo)
        {
            return (NB_Store_AddressInfo) CBO.FillObject(DataProvider.Instance().UpdateNB_Store_Address(objInfo.AddressID, objInfo.PortalID, objInfo.UserID, objInfo.AddressDescription, objInfo.AddressName, objInfo.AddressName2, objInfo.Address1, objInfo.Address2, objInfo.City, objInfo.RegionCode, objInfo.CountryCode, objInfo.PostalCode, objInfo.Phone1, objInfo.Phone2, objInfo.PrimaryAddress, objInfo.CreatedByUser, objInfo.CreatedDate, objInfo.OrderID, objInfo.CompanyName, objInfo.Extra1, objInfo.Extra2, objInfo.Extra3, objInfo.Extra4), typeof(NB_Store_AddressInfo));
        }

        public void UpdateObjOrderDetail(NB_Store_OrderDetailsInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_OrderDetails(objInfo.OrderDetailID, objInfo.OrderID, objInfo.Quantity, objInfo.UnitCost, objInfo.ModelID, objInfo.OptCode, objInfo.ItemDesc, objInfo.Discount, objInfo.Tax, objInfo.ProductURL, objInfo.PurchaseCost, objInfo.CartXMLInfo);
        }

        public void UpdateObjOrderStatus(NB_Store_OrderStatusInfo objInfo)
        {
            DataProvider.Instance().UpdateNB_Store_OrderStatus(objInfo.OrderStatusID, objInfo.Lang, objInfo.OrderStatusText, objInfo.ListOrder);
        }
    }
}

